#!/bin/bash
#
# 4shared.com module
# Copyright (c) 2010-2012 Plowshare team
#
# This file is part of Plowshare.
#
# Plowshare is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Plowshare is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Plowshare.  If not, see <http://www.gnu.org/licenses/>.

MODULE_VEEHD_REGEXP_URL="http://\(www\.\)\?veehd\.com/video/"

MODULE_VEEHD_DOWNLOAD_OPTIONS=""
MODULE_VEEHD_DOWNLOAD_RESUME=yes
MODULE_VEEHD_DOWNLOAD_FINAL_LINK_NEEDS_COOKIE=no

MODULE_VEEHD_LIST_OPTIONS=""

veehd_download() {

local COOKIEFILE=$1
local URL=$2
	
	login=$(curl -c test -d 'uname=jack44556677&pword=bullshit&submit=Login&terms=on&remember_me=on' http://veehd.com/login)
	
	Pageinit=$(curl -b test "$URL") || return

	FILE_URL=$(echo "$Pageinit" | grep "&do=d" | strip | replace "\$("#playeriframe").attr({src : " "")
	FILE_URL=${FILE_URL:32}
	FILE_URL=${FILE_URL%????}

	FILE_URL=$(echo "http://veehd.com$FILE_URL")
	VPI_LINK=$FILE_URL

	FILE_URL=$(curl -b test -e "$URL" "$FILE_URL") # | grep "veehd.com" | parse_attr 'href' | strip)

	FILE_URL=$(echo "$FILE_URL" | grep /va)
	FILE_URL=${FILE_URL##*src=\"}
	FILE_URL=${FILE_URL%%\" width*}
	FILE_URL=$(echo "http://veehd.com$FILE_URL")
	FILE_URL=$(curl -b test -e "$VPI_LINK" "$FILE_URL") # | grep "veehd.com" | parse_attr 'href' | strip)

	FILE_URL=$(curl -H "Cache-Control: max-age=0" -b test -e "$URL" "$VPI_LINK") # | grep "veehd.com" | parse_attr 'href' | strip)

	
	FILE_URL=$(echo $FILE_URL | grep "veehd.com" | parse_attr 'href')
	
	FILENAME=$(echo "$Pageinit" | parse_tag 'title')
	FILENAME=${FILENAME%?????????}
	FILENAME=$(echo "$FILENAME.avi")
	
	rm -rf test
	
	echo "$FILE_URL"
	echo "$FILENAME"
}
